/**
 * Created by Chuch on 2017/3/1.
 */
angular.module('myApp')


    .controller('queryCtrl',['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'limitToFilter', 'filterFilter', 'myConstant', 'dateFilter', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                               CacheService, limitToFilter, filterFilter, myConstant, dateFilter,DateCalculateService) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            vm.sumObject = {};
            //查询条 件
            vm.queryBean = {};

            //缓存数据初始化(需要缓存的key请自定义)
            /*数据格式{
             key1 :value1,
             key2:value2
             }*/
            vm.cached = {
                /*OPCIF_AUTH_STATUS_TYPE:{},*/
                /*GENDER:[{"key":"M","value":"男"},{"key":"F","value":"nv"}]*/
            };


            CacheService.initCache(vm.cached);
            vm.STATUS_GENDER=[{"key":"M","value":"男"},{"key":"F","value":"女"}];
            vm.APPLY_STATUS=[{"key":"APPLY_SUCCESS","value":"待审核"},{"key":"SUCCESS","value":"认证通过"},{"key":"FAIL","value":"认证失败"}];


            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };
            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;
            vm.auth = auth;
            vm.show = show;
            vm.checkStartDay=checkStartDay;
            vm.checkEndDay=checkEndDay;
            vm.getApplyDesc =getApplyDesc;

            function  getApplyDesc(status){
                for(var i=0;i<vm.APPLY_STATUS.length;i++){
                    if(vm.APPLY_STATUS[i].key==status){
                        return vm.APPLY_STATUS[i].value;
                    }
                }
            }
            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                //var postData = angular.copy(vm.queryBean);

                //组织参数：查询条件 + 分页数据
                var postData = {};
                postData.queryBean = vm.queryBean;
                postData.pageSize = vm.pagination.pageSize;
                postData.pageNum = vm.pagination.pageNum;
                $http.post(vm.url, postData,config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        //alert(JSON.stringify(data));
                        if (data.success) {
                            vm.pagination = data.object;
                        } else {
                            alert(data.msg);
                        }
                    });

            };

            function resetForm() {
                vm.queryBean = {};
            }
            function checkStartDay(startDate,endDate) {
                if(!isNull(startDate) && !isNull(endDate)){
                    var startD = new Date(startDate);
                    var endD   = new Date(endDate);
                    if(startD > endD){
                        alert("日期选择错误！请重新选择!");
                        vm.queryBean.applyStartDate = null;
                        vm.queryBean.applyEndDate = DateCalculateService.getToday();
                        return false;
                    }
                }

            }

            function checkEndDay(startDate,endDate) {
                if(!isNull(startDate) && !isNull(endDate)) {
                    var startD = new Date(startDate);
                    var endD = new Date(endDate);
                    if (endD < startD) {
                        alert("日期选择错误！请重新选择!");
                        vm.queryBean.applyStartDate = null;
                        vm.queryBean.applyEndDate = DateCalculateService.getToday();
                        return false;
                    }
                }
            }


            function show(bean) {
                //var postData = angular.copy(bean);
                vm.postData = {"authApplyId": bean.authApplyId};
                $http.post(config.ctx + '/opcif/idcardAuthentication/selectOneMsg', $httpParamSerializerJQLike(vm.postData))
                    .then(function (response) {
                        if (response.data.success) {
                            //alert(response.data);
                            var data = response.data;
                            //alert(JSON.stringify(data));
                            console.log(data);
                            vm.authData = data.object;
                            OpenService({
                                modalTitle: '处理信息',
                                modalBody: 'toDetail',
                                url: 'toDetail',
                                item: vm.authData,
                                statusGender: vm.STATUS_GENDER,
                                cacheData: {"entityNo": bean.operatorNo, "authStatus": bean.authStatus},
                                cached: {
                                    COMPLAINT_TYPE_FOR_RESERVE: vm.cached.COMPLAINT_TYPE_FOR_RESERVE
                                }
                            }, function () {
                                vm.queryDetail();
                            }, "handleComplaitInfo", 'myModalNoSave.html');

                        } else {
                            alert(response.data.msg);
                        }
                    }, function (response) {
                        alert("操作失败"+response.status);
                    })
            }


            function auth(bean) {
                //var postData = angular.copy(bean);
                vm.postData={"authApplyId":bean.authApplyId};
                $http.post(config.ctx + '/opcif/idcardAuthentication/selectOneMsg', $httpParamSerializerJQLike(vm.postData))
                    .then(function (response) {
                        if (response.data.success) {
                            //alert(response.data);
                            var data = response.data;
                            //alert(JSON.stringify(data));
                            console.log(data);
                            vm.authData=data.object;
                            OpenService({
                                modalTitle: '处理信息',
                                modalBody: 'toDetail',
                                url: 'toDetail',
                                item: vm.authData,
                                statusGender:vm.STATUS_GENDER,
                                cacheData:{"entityNo":bean.operatorNo,"authStatus":bean.authStatus},
                                cached: {
                                    COMPLAINT_TYPE_FOR_RESERVE:vm.cached.COMPLAINT_TYPE_FOR_RESERVE
                                }
                            },function(){
                                vm.queryDetail();
                            },"handleComplaitInfo",'checkContent.html');

                        } else {
                            alert(response.data.msg);
                        }
                    }, function (response) {
                        alert("操作失败"+response.status);
                    })
            }
            function isNull(data){
                return (data == "" || data == undefined || data == null);
            }

        }]);


angular.module('myApp').controller('handleComplaitInfo',['$scope', '$uibModalInstance', 'modalItem', '$http',
    '$httpParamSerializerJQLike', '$log', 'dateFilter', 'myConstant', function ($scope, $uibModalInstance, modalItem, $http,
                                                                                $httpParamSerializerJQLike, $log, dateFilter, myConstant) {

        var vm = $scope.vm = {
            modalTitle: 'please change the title',
            modalBody: '#',
            url: '',
            item: {},
            statusGender:{},
            cacheData:{},
        }
        vm.cached = {
            GENDER:[{"key":"M","value":"男"},{"key":"F","value":"nv"}]
        };

        vm = $scope.vm = modalItem;
        vm.constant = myConstant;
        vm.itemBean =angular.copy(vm.item);
        $scope.save = function () {

            vm.postData = angular.copy(vm.item);
            $log.debug("postData",vm.postData);
            $http.post(vm.url, $httpParamSerializerJQLike(vm.postData))
                .then(function (response) {
                    if (response.data.success) {
                        alert("操作成功");
                        $uibModalInstance.close(vm.item);
                    } else {
                        alert(response.data.msg);
                    }
                }, function (response) {
                    alert("操作失败");
                    alert(response.status);
                })
        };




        //点击通过或不通过按钮
        $scope.check = function (status) {
            vm.url =  config.ctx + '/opcif/idcardAuthentication/toAuth';
            vm.item.authStatus=status;
            vm.item.entityNo=vm.cacheData.entityNo;
            var result =checkParams(status);
            if(result ==0){
                return;
            }
            var postData = {};
            postData.gender =vm.item.gender;
            postData.address =vm.item.address;
            postData.nationality =vm.item.nationality;
            postData.birthday =vm.item.birthday;
            postData.certStartDate =vm.item.certStartDate;
            postData.certExpiryDate =vm.item.certExpiryDate;
            postData.issueOffice =vm.item.issueOffice;
            postData.authStatus =vm.item.authStatus;
            postData.description =vm.item.description;
            postData.entityNo =vm.item.entityNo;
            postData.authApplyId =vm.item.authApplyId;
            //alert(JSON.stringify(postData));
            $http.post(vm.url, postData,config.jsonHeader)
            /*$http.post(vm.url, $httpParamSerializerJQLike(vm.item))*/
                .then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        $scope.cancel();


                    } else {
                        alert(data.msg);
                        $scope.cancel();
                    }
                });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };


        function checkParams(status){
            if(status == "SUCCESS"){

                if(isNull(vm.item.gender)){
                    alert("请选择性别!");
                    return 0;
                }
                if(isNull(vm.item.birthday)){
                    alert("出生日期不能为空!");
                    return 0;
                }
                if(isNull(vm.item.certStartDate)){
                    alert("签发日不能为空!");
                    return 0;
                }
                if(isNull(vm.item.certExpiryDate)){
                    alert("截止日不能为空!");
                    return 0;
                }
                if(isNull(vm.item.nationality)){
                    alert("民族不能为空!");
                    return 0;
                }
                if(vm.item.nationality.length>8){
                    alert("民族字数长度不得大于8!");
                    return 0;
                }
                if(isNull(vm.item.issueOffice)){
                    alert("签发机关不能为空!");
                    return 0;
                }
                if(vm.item.issueOffice.length<1 || vm.item.issueOffice.length>30){
                    alert("签发机关字数长度[1,30]!");
                    return 0;
                }
                if(isNull(vm.item.address)){
                    alert("住址不能为空!");
                    return 0;
                }
                if(vm.item.address.length<1 || vm.item.address.length>50){
                    alert("住址字数长度[1,50]!");
                    return 0;
                }
                if(isNull(vm.item.description)){
                    alert("备注不能为空!");
                    return 0;
                }
                if(vm.item.description.length<3 || vm.item.description.length>100){
                    alert("备注字数长度[3,100]!");
                    return 0;
                }
            }else{
                if(isNull(vm.item.description)){
                    alert("备注不能为空!");
                    return 0;
                }
                if(vm.item.description.length<3 || vm.item.description.length>100){
                    alert("备注字数长度[3,100]!");
                    return 0;
                }
            }
            return 1;

        }

        function isNull(data){
            return (data == "" || data == undefined || data == null);
        }




    }]);